package controllers.admin;

import java.util.ArrayList;
import java.util.List;

import models.Administrator;
import models.Message;
import models.Order;
import models.OrderItem;
import models.QueryBean;
import models.RemittanceConfirm;
import models.BaseEntity.Status;
import models.Message.MessageStatus;
import models.Message.MessageType;
import models.RemittanceConfirm.ConfirmStatus;
import play.mvc.Controller;
import play.mvc.With;
import controllers.GlobalVariables;
import controllers.Secure;
import controllers.ValidationUtil;

@With(Secure.class)
public class Messages extends Controller {

	/**
	 * 消息列表
	 * 
	 * @param queryBean
	 */
	public static void indexQuestions(QueryBean queryBean){
		if(queryBean == null) {
			queryBean = new QueryBean();
			queryBean.isDesc = true;
		}
    	StringBuilder hql = new StringBuilder("id != null and target = null ");
    	List<Object> objs = new ArrayList<Object>();
    	
    	hql.append("and type = ? ");
    	objs.add(MessageType.QUESTION);
    	
    	if(!ValidationUtil.isEmpty(queryBean.keyword)) {
    		hql.append("and (title like ? or content like ? or author like ?) ");
    		objs.add("%"+queryBean.keyword+"%");
    		objs.add("%"+queryBean.keyword+"%");
    		objs.add("%"+queryBean.keyword+"%");
    	}
    	
    	if(!ValidationUtil.isEmpty(queryBean.orderby)) {
    		hql.append(" order by " + queryBean.orderby);
    		if(queryBean.isDesc) 
    			hql.append(" desc ");
    		else
    			hql.append(" asc ");
    	} else {
    		hql.append(" order by createAt desc, lastModifiedAt desc ");
    	}
		
		//页数计算
		queryBean.totalQuantity = Message.count(hql.toString(), objs.toArray());
		queryBean.totalPage = (queryBean.totalQuantity/queryBean.quantityEachPage + 1);
		if(queryBean.totalPage < queryBean.currentPage) {
			queryBean.currentPage = queryBean.totalPage;
		}
		
		List<Message> msgs = Message.find(hql.toString(), objs.toArray()).fetch(
				(int)queryBean.currentPage, queryBean.quantityEachPage);
		render(msgs, queryBean);
	}
	
	/**
	 * 删除消息
	 */
	public static void deleteMessage(long id){
		//查找该留言
		Message msg = Message.findById(id);
		
		//删除留言
		Message.em().remove(msg);
		
//		QueryBean queryBean = new QueryBean();
		
//		render("@indexQuestions",queryBean);
	}
	
	/**
	 * 审核通过用户留言
	 */
	public static void updatePassMessage(long id){
		String username = session.get(GlobalVariables.SECURE_ADMINNAME);
		Administrator admin = Administrator.find("byUsername", username).first();
		
		Message msg = Message.findById(id);
		msg.messageStatus = MessageStatus.PASS;
		msg.resetModifier(admin);
		msg.save();
		
		QueryBean queryBean = new QueryBean();
		
		render("@indexQuestions",queryBean);
	}
	
	/**
	 * 审核不通过用户留言
	 */
	public static void updateFailMessage(long id){
		String username = session.get(GlobalVariables.SECURE_ADMINNAME);
		Administrator admin = Administrator.find("byUsername", username).first();
		
		Message msg = Message.findById(id);
		msg.messageStatus = MessageStatus.FAIL;
		msg.resetModifier(admin);
		msg.save();
		
		QueryBean queryBean = new QueryBean();
		
		render("@indexQuestions",queryBean);
	}
	
	/**
	 * 回复用户留言。
	 */
	public static void createRebackMessage(long rebackid,String rebackContent){
		String username = session.get(GlobalVariables.SECURE_ADMINNAME);
		Administrator admin = Administrator.find("byUsername", username).first();
		
		Message msg = Message.findById(rebackid);
		Message reply = new Message();
		reply.status = Status.ENABLE;
		reply.target = msg;
		reply.content = rebackContent;
		reply.setCreator(admin);
		reply.resetModifier(admin);
		reply.toUser = msg.createByUser;
		reply.save();
		
		QueryBean queryBean = new QueryBean();
		
		render("@indexQuestions",queryBean);
	}
	
	/**
	 * 跳转到退款消息页面。
	 */
	public static void indexRefund() {
		render();
	}
	
	/**
	 * 退款消息列表。
	 */
	public static void refundTable(QueryBean queryBean) {
		if(queryBean == null) {
			queryBean = new QueryBean();
			queryBean.isDesc = true;
		}
    	StringBuilder hql = new StringBuilder("id != null and target = null ");
    	List<Object> objs = new ArrayList<Object>();
    	hql.append("and status != ? ");
    	objs.add(Status.DISABLED);
    	
    	hql.append("and type = ? ");
    	objs.add(MessageType.REFUND);
    	
    	if(!ValidationUtil.isEmpty(queryBean.keyword)) {
    		hql.append("and (content like ? or author like ?) ");
    		objs.add("%"+queryBean.keyword+"%");
    		objs.add("%"+queryBean.keyword+"%");
    	}
    	
    	if(!ValidationUtil.isEmpty(queryBean.orderby)) {
    		hql.append(" order by " + queryBean.orderby);
    		if(queryBean.isDesc) 
    			hql.append(" desc ");
    		else
    			hql.append(" asc ");
    	} else {
    		hql.append(" order by createAt desc, lastModifiedAt desc ");
    	}
		
		//页数计算
		queryBean.totalQuantity = Message.count(hql.toString(), objs.toArray());
		queryBean.totalPage = (queryBean.totalQuantity/queryBean.quantityEachPage + 1);
		if(queryBean.totalPage < queryBean.currentPage) {
			queryBean.currentPage = queryBean.totalPage;
		}
		
		List<Message> msgs = Message.find(hql.toString(), objs.toArray()).fetch(
				(int)queryBean.currentPage, queryBean.quantityEachPage);
		render(msgs, queryBean);
	}
	
	/**
	 * 跳转到退款消息页面。
	 */
	public static void indexOthers() {
		render();
	}
	
	/**
	 * 退款消息列表。
	 */
	public static void othersTable(QueryBean queryBean, MessageType messageType) {
		if(queryBean == null) {
			queryBean = new QueryBean();
			queryBean.isDesc = true;
		}
    	StringBuilder hql = new StringBuilder("id != null and target = null ");
    	List<Object> objs = new ArrayList<Object>();
    	hql.append("and status != ? ");
    	objs.add(Status.DISABLED);
    	
    	if(messageType == null) {
    		hql.append("and type in (?, ?, ?) ");
        	objs.add(MessageType.CONSULTING);
        	objs.add(MessageType.SUGGEST);
        	objs.add(MessageType.OTHER);
    	} else {
    		hql.append("and type = ? ");
    		objs.add(messageType);
    	}
    	
    	
    	if(!ValidationUtil.isEmpty(queryBean.keyword)) {
    		hql.append("and (content like ? or author like ?) ");
    		objs.add("%"+queryBean.keyword+"%");
    		objs.add("%"+queryBean.keyword+"%");
    	}
    	
    	if(!ValidationUtil.isEmpty(queryBean.orderby)) {
    		hql.append(" order by " + queryBean.orderby);
    		if(queryBean.isDesc) 
    			hql.append(" desc ");
    		else
    			hql.append(" asc ");
    	} else {
    		hql.append(" order by createAt desc, lastModifiedAt desc ");
    	}
		
		//页数计算
		queryBean.totalQuantity = Message.count(hql.toString(), objs.toArray());
		queryBean.totalPage = (queryBean.totalQuantity/queryBean.quantityEachPage + 1);
		if(queryBean.totalPage < queryBean.currentPage) {
			queryBean.currentPage = queryBean.totalPage;
		}
		
		List<Message> msgs = Message.find(hql.toString(), objs.toArray()).fetch(
				(int)queryBean.currentPage, queryBean.quantityEachPage);
		render(msgs, queryBean);
	}
}